using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WikiApp.Pages;
using WikiApp.Services;

namespace WikiApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            try
            {
                InitializeComponent();
                if (MainFrame == null)
                {
                    throw new InvalidOperationException("MainFrame is null after initialization");
                }

                NavigationService.Instance.Initialize(MainFrame);
                
                try
                {
                    CategoryPageFactory.Initialize();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"初始化CategoryPageFactory时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                        "初始化错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    throw;
                }

                try
                {
                    var page = CategoryPageFactory.CreatePage("Windows");
                    if (page == null)
                    {
                        throw new InvalidOperationException("CategoryPageFactory.CreatePage returned null");
                    }
                    MainFrame.Navigate(page);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"创建页面时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                        "页面创建错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    throw;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"主窗口初始化时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                    "严重错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Application.Current.Shutdown();
            }
        }

        private void HomeButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NavigationService.Instance.NavigateTo(CategoryPageFactory.CreatePage("Home"));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导航到主页时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                    "导航错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void WindowsButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NavigationService.Instance.NavigateTo(CategoryPageFactory.CreatePage("Windows"));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导航到Windows页面时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                    "导航错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void AndroidButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NavigationService.Instance.NavigateTo(CategoryPageFactory.CreatePage("Android"));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导航到Android页面时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                    "导航错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void DjangoButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NavigationService.Instance.NavigateTo(CategoryPageFactory.CreatePage("Django"));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导航到Django页面时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                    "导航错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void GiteeButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NavigationService.Instance.NavigateTo(CategoryPageFactory.CreatePage("Gitee"));
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导航到Gitee页面时出错：\n{ex.Message}\n\n{ex.StackTrace}", 
                    "导航错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
} 